package com.fyzk.activity.special;

import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.fyzk.base.BaseActivity;
import com.fyzk.activity.lottery.gaopin.AnHuiK3Activity;
import com.fyzk.activity.lottery.gaopin.GD11x5Activity;
import com.fyzk.activity.lottery.gaopin.JLK3Activity;
import com.fyzk.activity.lottery.gaopin.JSK3Activity;
import com.fyzk.activity.lottery.gaopin.NXK3Activity;
import com.fyzk.activity.lottery.gaopin.SD11x5Activity;
import com.fyzk.activity.lottery.gaopin.SSCActivity;
import com.fyzk.activity.lottery.shuzicai.DLTActivity;
import com.fyzk.activity.lottery.shuzicai.FC3DActivity;
import com.fyzk.activity.lottery.shuzicai.PL3Activity;
import com.fyzk.activity.lottery.shuzicai.PL5Activity;
import com.fyzk.activity.lottery.shuzicai.QLCActivity;
import com.fyzk.activity.lottery.shuzicai.QXCActivity;
import com.fyzk.activity.lottery.shuzicai.SSQActivity;
import com.fyzk.activity.lottery.zc_new.OldFootballBQCActivity_New;
import com.fyzk.activity.lottery.zc_new.OldFootballJQCActivity_New;
import com.fyzk.activity.special.virtual_lottery.Virtual_KLSFActivity;
import com.fyzk.bean.BetRecordResponse;
import com.fyzk.bean.LotteryLayoutItem;
import com.fyzk.data.AppLotteryInfos;
import com.tyjc.lottery.R;
import com.fyzk.net.Commands;
import com.fyzk.utils.L;
import com.fyzk.utils.LotteryIDUtils;
import com.fyzk.view.LotteryItemView;
import com.fyzk.view.TopTitleView;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

import http.Common.CommandUtil;
import http.StringResultBean;

/**
 * Created by fanxs11 on 17/1/3.
 *
 * 虚拟大厅
 */

@ContentView(R.layout.ac_virtualhall)
public class VirtualHallActivity extends BaseActivity {

    @ViewInject(R.id.virtualhall_top_rl)
    private TopTitleView topTitleView;

    @ViewInject(R.id.virtualhall_goucai_gv)
    private GridView gv;
    private ArrayList<LotteryItemView> lotteryItemViews = new ArrayList<LotteryItemView>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initView();

        requestLotteryTypeLayout();
    }

    /**
     * 初始化控件
     */
    private void initView() {
        topTitleView.setOnTitleClickListener(this);

        gv.setAdapter(lotteryAdapter);


    }

    /**
     * 查询虚拟钱未开期列表
     */
    private void findNextVirtualPhaseList() {
        JSONObject params = new JSONObject();
        try {
            params.put("lotteryType", LotteryIDUtils.KLSF_LOTTERY_ID_INT);
            params.put("num", 10);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        httpPostJson(CommandUtil.FINDNEXTVIRTUALPHASELIST, Commands.FINDNEXTVIRTUALPHASELIST, params, false);
    }

    private void requestLotteryTypeLayout() {
        JSONObject json = new JSONObject();
        httpPostJson(CommandUtil.LOTTERYTYPELAYOUT, Commands.LOTTERYTYPELAYOUT, json, false);
    }

    private BetRecordResponse betRecordResponse;
    private List<LotteryLayoutItem> lotteryLayoutItemList;

    @Override
    protected void onSuccess(int requestCode, StringResultBean resultBean, String result) {
        super.onSuccess(requestCode, resultBean, result);
        switch (requestCode) {
            case CommandUtil.LOTTERYTYPELAYOUT:
                lotteryLayoutItemList = JSON.parseArray(resultBean.getData(),LotteryLayoutItem.class);
                initLotteryItemViewList();
                lotteryAdapter.notifyDataSetChanged();
                break;
        }
    }

    @Override
    protected void onFailure(int requestCode, String errorcode,String message) {
        switch (requestCode){
            case CommandUtil.LOTTERYTYPELAYOUT:
//                lotteryLayoutItemList = JSON.parseArray( Constants.data,LotteryLayoutItem.class);
                initLotteryItemViewList();
                lotteryAdapter.notifyDataSetChanged();
                break;
        }
    }

    private void initLotteryItemViewList() {
//        for (LotteryLayoutItem info : lotteryLayoutItemList) {
//            LotteryItemView itemVIew = new LotteryItemView(getContext(),
//                    AppLotteryInfos.getLogoId(info.getLotteryType()));
//            itemVIew.setLotteryLogo( AppLotteryInfos.getLogoId(info.getLotteryType()));
//            itemVIew.setLotteryName(info.getLotteryTypeName());
//            AppLotteryInfos.setLotteryName(info.getLotteryType(),info.getLotteryTypeName());
//            itemVIew.setLotteryId(info.getLotteryType());
//            itemVIew.isShowPeriod(false);
//            itemVIew.setId(info.getLotteryType());// 以lotteryId 为新View 的id值
//            itemVIew.setDescription(info.getDescription());
//            itemVIew.setOnClickListener(this);
//            if(AppLotteryInfos.getVirtual_lotterys().contains(Integer.valueOf(info.getLotteryType()))){
//                lotteryItemViews.add(itemVIew);
//            }
//        }
        for (LotteryLayoutItem info : lotteryLayoutItemList) {
            LotteryItemView itemVIew = new LotteryItemView(this,
                    AppLotteryInfos.getLogoId(info.getLotteryType()));
            info.setLogoRes(AppLotteryInfos.getLogoId(info.getLotteryType()));
            itemVIew.setId(info.getLotteryType());// 以lotteryId 为新View 的id值
            itemVIew.setLotteryInfo(info);
            itemVIew.setOnClickListener(this);
            if (AppLotteryInfos.getcurrent_version_lotterys().contains(info
                    .getLotteryType())) {
                lotteryItemViews.add(itemVIew);
            }
        }


    }

    BaseAdapter lotteryAdapter = new BaseAdapter() {

        @Override
        public View getView(int arg0, View arg1, ViewGroup arg2) {
            // TODO Auto-generated method stub
            if (arg0 == lotteryItemViews.size()) {
                Button button = new Button(getContext());
                button.setText("投注列表");
                button.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(getContext(), VirtualBetActivity.class);
                        startActivity(intent);
                    }
                });
                return button;
            } else {

                return lotteryItemViews.get(arg0);
            }
        }

        @Override
        public long getItemId(int arg0) {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public Object getItem(int arg0) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return lotteryItemViews.size()+1;
        }
    };

    @Override
    public void onClick(View arg0) {
        Intent intent = new Intent();
        switch (arg0.getId()) {
            case LotteryIDUtils.SSQ_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSQActivity.class);
                break;
            case LotteryIDUtils.DLT_LOTTERY_ID_INT:
                intent.setClass(getContext(), DLTActivity.class);
                break;
            case LotteryIDUtils.QXC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QXCActivity.class);
                break;
            case LotteryIDUtils.PL3_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL3Activity.class);
                break;
            case LotteryIDUtils.PL5_LOTTERY_ID_INT:
                intent.setClass(getContext(), PL5Activity.class);
                break;
            case LotteryIDUtils.FC3D_LOTTERY_ID_INT:
                intent.setClass(getContext(), FC3DActivity.class);
                break;
            case LotteryIDUtils.ZCBQC_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballBQCActivity_New.class);
                break;
            case LotteryIDUtils.QLC_LOTTERY_ID_INT:
                intent.setClass(getContext(), QLCActivity.class);
                break;
            case LotteryIDUtils.ZCJQC_LOTTERY_ID_INT:
                intent.setClass(getContext(), OldFootballJQCActivity_New.class);
                break;
            case LotteryIDUtils.SD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), SD11x5Activity.class);
                break;
            case LotteryIDUtils.GD11XUAN5_LOTTERY_ID_INT:
                intent.setClass(getContext(), GD11x5Activity.class);
                break;
            case LotteryIDUtils.CQSSC_LOTTERY_ID_INT:
                intent.setClass(getContext(), SSCActivity.class);
                break;
            case LotteryIDUtils.NXK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), NXK3Activity.class);
                break;
            case LotteryIDUtils.ANHUIK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), AnHuiK3Activity.class);
                break;
            case LotteryIDUtils.JSK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), JSK3Activity.class);
                break;
            case LotteryIDUtils.JLK3_LOTTERY_ID_INT:
                intent.setClass(getContext(), JLK3Activity.class);
                break;
            case LotteryIDUtils.KLSF_LOTTERY_ID_INT:
                intent.setClass(getContext(), Virtual_KLSFActivity.class);
                break;

        }
        try {
            startActivity(intent);
        } catch (ActivityNotFoundException e) {
            L.e(TAG, e.getMessage());
        }
    }

}
